package com.zlfund.cat.zjgsmk.ui.channel;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.adapter.AdapterFactory;
import com.zlfund.cat.zjgsmk.adapter.ListViewAdapter;
import com.zlfund.cat.zjgsmk.adapter.ListViewHolder;
import com.zlfund.cat.zjgsmk.adapter.ListViewOneTypeAdapterHelper;
import com.zlfund.cat.zjgsmk.constant.Constants;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.util.BitmapHelper;
import com.zlfund.cat.zjgsmk.util.GlideUtil;
import com.zlfund.cat.zjgsmk.util.Logger;
import com.zlfund.cat.zjgsmk.util.filepick.FilePick;
import com.zlfund.cat.zjgsmk.util.filepick.LocalMediaImage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PhotoActivity extends BaseActivity {

    @BindView(R.id.gv)
    GridView mGv;
    @BindView(R.id.btn_directory)
    Button mBtnDirectory;
    private ListViewAdapter<LocalMediaImage> mAdapter;
    private FilePick mPicker;
    private boolean mCanReadSDCard;
    private boolean mCanWriteSDCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, Constants.WRITE_EXTERNAL_PERMISSION);
        }else {
            mPicker.filePick();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Constants.WRITE_EXTERNAL_PERMISSION) {
            mCanReadSDCard = grantResults[1] == PackageManager.PERMISSION_GRANTED;
            mCanWriteSDCard = grantResults[0] == PackageManager.PERMISSION_GRANTED;
            mPicker.filePick();
        }
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_photo);
    }

    @Override
    protected void setListener() {
        mBtnDirectory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, AlbumActivity.class).putParcelableArrayListExtra("album",  mPicker.getDirectories()));
            }
        });
    }

    @Override
    protected void initData() {
        mAdapter = AdapterFactory.singleTon().createOneTypeLvAdapter(mContext, new ListViewOneTypeAdapterHelper<LocalMediaImage>(R.layout.item_photo) {
            @Override
            public void initViews(ListViewHolder holder, int position, LocalMediaImage localMediaImage) {
                String thumbPath = localMediaImage.getThumbPath();
                if (thumbPath == null) {
                    thumbPath = localMediaImage.getFilePath();
                }
                BitmapHelper.setBitmap(thumbPath, holder.getView(R.id.iv, ImageView.class));
//                GlideUtil.loadFile(mContext,new File(thumbPath),holder.getView(R.id.iv, ImageView.class));
            }
        });
        mGv.setAdapter(mAdapter);
        mPicker = new FilePick();
        mPicker.setOnFileListChangedListener(new FilePick.OnFileListChangeListener() {
            @Override
            public void pickStart() {

            }

            @Override
            public void fileListChanged(final List<LocalMediaImage> list) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.setList(list);
                    }
                });
            }

            @Override
            public void pickFinish() {

            }
        });
    }
}
